package com.example.annotations.configuration;

import org.springframework.beans.factory.config.YamlPropertiesFactoryBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.io.ClassPathResource;

import java.util.List;
import java.util.Map;


@ConfigurationProperties("config.test")
/**
 * @ConfigurationProperties("config.test") 通常与@EnableConfigurationProperties注解一起使用
 * 作用：从所有yaml文件中读取前缀为config.test里的数据，在该类实例化时，将其数据赋值给对应的字段。
 * 注意：本身没有实例化的功能，但@EnableConfigurationProperties具有实例化的功能
 */
public class TestProperties {
    int num;
    String str;
    Map<String,Integer> map;
    List<Obj> objList;
    class Obj{
        String[] name;
        int[] nums;
    }

    @Bean
    public static PropertySourcesPlaceholderConfigurer properties() {
        PropertySourcesPlaceholderConfigurer propertySourcesPlaceholderConfigurer = new PropertySourcesPlaceholderConfigurer();
        YamlPropertiesFactoryBean yaml = new YamlPropertiesFactoryBean();
        yaml.setResources(new ClassPathResource("config/application-test-properties.yaml"));
        propertySourcesPlaceholderConfigurer.setProperties(yaml.getObject());

        return propertySourcesPlaceholderConfigurer;
    }

    public int getNum() {
        return num;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public String getStr() {
        return str;
    }

    public void setStr(String str) {
        this.str = str;
    }

    public Map<String, Integer> getMap() {
        return map;
    }

    public void setMap(Map<String, Integer> map) {
        this.map = map;
    }

    public List<Obj> getObjList() {
        return objList;
    }

    public void setObjList(List<Obj> objList) {
        this.objList = objList;
    }
}
